<?php

namespace App\Kuafu\Shop\Validate;

use App\Exceptions\CustomValidateException;
use App\Http\Validate\AbstractBaseValidate;
use App\Kuafu\Shop\Model\CameraModel;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class DeviceValidate extends AbstractBaseValidate
{

    /**
     * @inheritDoc
     */
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [
            "shopId"   => "required|int|min:0",
            "serialNo" => [
                "required",
                "string",
                "size:16",
                Rule::unique("shop_camera", "serialNo")->where("isDeleted", 0)
            ],
            "sort"     => self::intInValidateBuild(true, CameraModel::TV_POSITION_TEXT)
        ];
    }

    /**
     * @inheritDoc
     */
    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [
            "shopId"     => "门店ID",
            "serialNo"   => "设备唯一码",
            "deviceName" => "设备名称",
            "name"       => "设备名称"
        ];
    }

    /**
     * @inheritDoc
     */
    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    /**
     * @inheritDoc
     */
    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [
            "serialNo.unique" => "当前设备已绑定",
            "serialNo.exists" => "设备尚未绑定"
        ];
    }


    /**
     * 绑定设备
     *
     * @param array $params
     * @return MessageBag|object
     * @throws CustomValidateException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function bindValidate(array $params)
    {
        return self::getInstance()->validate(
            ["serialNo", "shopId", "sort"],
            $params,
            null,
            true,
            function ($params) {
                $params["name"] = CameraModel::TV_POSITION_TEXT[$params["sort"]];
                return $params;
            }
        );
    }

    public static function bindMMCValidate(array $params)
    {
        return self::getInstance()->validate(
            ["serialNo", "shopId", "name"],
            $params,
            [
                'name' => [
                    'required'
                ],
                'serialNo' => [
                    "required",
                    "string",
                    Rule::unique("shop_camera", "serialNo")->where("isDeleted", 0)
                ]
            ],
            true,
            function ($params) {
                return $params;
            }
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function rmBindValidate(array $params)
    {
        return self::getInstance()->validate(
            ["shopId", "serialNo"],
            $params,
            [
                "serialNo" => Rule::exists("shop_camera", "serialNo")->where(function ($query) {
                    $shopId = request()->headers->get("shopId", 0);
                    $query->where("shopId", $shopId);
                })->where("isDeleted", 0)
            ]
        );
    }


    /**
     * 获取设备广告信息校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function deviceAdvValidate(array $params)
    {
        return self::getInstance()->validate(
            ["serialNo"],
            $params,
            [
                "serialNo" => [
                    "required",
                    "string",
                    "size:16",
//                    Rule::exists("shop_camera", "serialNo")->where("isDeleted", 0)
                ]
            ]
        );
    }

    /**
     * 设备编辑
     *
     * @param array $params
     * @return MessageBag|object
     * @throws CustomValidateException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function updateValidate(array $params)
    {
        return self::getInstance()->validate(
            ["serialNo", "shopId", "sort"],
            $params,
            null,
            true,
            function ($params) {
                $params["name"] = CameraModel::TV_POSITION_TEXT[$params["sort"]];
                return $params;
            }
        );
    }
}
